﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using FWF.Logging.Statistics;

using log4net.Core;

using NUnit.Framework;

namespace FWF.Logging.Test.Statistics
{
    /// <summary>
    /// 
    /// </summary>
    public class ManagedPerformanceCounterTest
    {
        private readonly InMemoryLogger _testLog = new InMemoryLogger();

        /// <summary>
        /// Setups this instance.
        /// </summary>
        [SetUp]
        public void Setup()
        {
            _testLog.Reset();
        }

        /// <summary>
        /// Determines whether [has messages from close].
        /// </summary>
        [Test]
        public void ReferToReadOnlyCounter()
        {
            // Create a reference to the Windows % Processor Time performance counter
            ManagedPerformanceCounter managedPerformanceCounter = ManagedPerformanceCounter.Refer(
                _testLog,
                "Processor",
                "% Processor Time",
                "_Total",
                TimeSpan.FromMilliseconds( 100 )
                );

            // Allow for the performance counter to query several times...
            Thread.Sleep( 300 );

            managedPerformanceCounter.Dispose();

            // Fetch Trace messages from the log that should have been written by the ManagedPerformanceCounter
            IEnumerable<string> listMessages = _testLog.GetMessagesForLog( Level.Trace );

            foreach ( string s in listMessages )
            {
                Debug.WriteLine( s );
            }

            var wrappingCollection = new List<string>( listMessages );

            Assert.IsNotEmpty( wrappingCollection );
            Assert.GreaterOrEqual(
                wrappingCollection.Count,
                1
                );
        }

    }
}
